package app.nbdevices.service.impl;



import app.nbdevices.service.IFaultService;
import app.service.DeviceManageService;
import app.service.FacilitiesManageService;
import app.service.UserManageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mess.warningmess.Bean4WarnMessInfo;
import com.mess.warningmess.WarningMass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import refactor.entity.Device;
import refactor.entity.EventLog;

import refactor.entity.Module;
import refactor.mapper.EventLogMapper;
import refactor.mapper.ModuleMapper;
import siotwx.entity.Facilities;
import siotwx.entity.Facmonitoring;
import siotwx.entity.User;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("FaultService")
public class FaultServiceImpl implements IFaultService {

    @Autowired
    @Qualifier("DeviceManageService")
    DeviceManageService deviceManageService;

    @Autowired
    @Qualifier("FacilitiesManageService")
    FacilitiesManageService facilitiesManageService;

    @Autowired
    @Qualifier("UserManageService")
    UserManageService userManageService;

    @Autowired
    ModuleMapper moduleMapper;

    @Autowired
    EventLogMapper eventLogMapper;


    @Override
    public int saveEventLog(String internetDevId, int typeId, int faultTypeId) {
        if(internetDevId.equals("38ecff03-03a7-4fcb-aaa4-41e85f70b5e4")){
            System.out.println("ddd");
        }
        long now_time = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"))*1000;
        Device d=deviceManageService.getDeviceByInternetDevId(internetDevId);
        Facilities f=deviceManageService.getFacById(d.getFacilitiesId());
        EventLog e=new EventLog();
        e.setDeviceId(d.getId());
        e.setModuleId(d.getModId());
        e.setFacilitiesId(d.getFacilitiesId());
        e.setUserId(f.getUser_id());
        e.setFaultTypeId(faultTypeId);
        e.setTypeId(typeId);
        e.setDeviceStatus("0");
        e.setEndTime(now_time);
        e.setFlag("0");
        e.setDoorStatus("");
        e.setElectricity("0");
        e.setExtension("");
        e.setStartTime(now_time);
        e.setResult("0");

        Map<String,Object> q=new HashMap<>(4);
        q.put("device_id",e.getDeviceId());
        q.put("facilities_id",e.getFacilitiesId());
        q.put("fault_type_id",e.getFaultTypeId());
        q.put("type_id",e.getTypeId());

        EventLog eventLog=eventLogMapper.selectOne(new QueryWrapper<EventLog>().allEq(q));
        if(eventLog==null){
            System.out.println(e);
            return eventLogMapper.insert(e);
        }else if(eventLog.getResult().equals("1")){
            eventLog.setResult("0");
            eventLog.setDeviceStatus("0");
            eventLog.setStartTime(now_time);
            eventLog.setEndTime(now_time);
            return eventLogMapper.updateById(eventLog);
        }
        return 1;
    }

    public static void main(String[] args) {
        long now_time = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
        System.out.println(now_time);
    }
    @Override
    public boolean updateEventLog(String internetDevId, ArrayList<Integer> faultTypeIds, String result) {
        Device d=deviceManageService.getDeviceByInternetDevId(internetDevId);
        if(d==null){
            return false;
        }
        Map<String,Object> p=new HashMap<>(2);
        p.put("device_id",d.getId());
        p.put("result","0");
        List<EventLog> eventLogs=eventLogMapper.selectList(new QueryWrapper<EventLog>().allEq(p));
        for(EventLog e:eventLogs){
            e.setDeviceStatus("1");
        }
        for(int j=0;j<eventLogs.size();j++){
            for(int i=0;i<faultTypeIds.size();i++){
                if(eventLogs.get(j).getFaultTypeId()==faultTypeIds.get(i)){
                    eventLogs.remove(j);
                }
            }
        }
        if(eventLogs.size()==0){
            return true;
       }
        return eventLogMapper.lotUpdate(eventLogs);
    }
    @Override
    public void sendWarnMess(String internetDeviceId, int eventType,String msg) {
        Device deviceResult=deviceManageService.getDeviceByInternetDevId(internetDeviceId);
        Facilities facilitiesResult = facilitiesManageService.get(deviceResult.getFacilitiesId());
        Module module = moduleMapper.selectOne(new QueryWrapper<Module>().eq("id", deviceResult.getModId()));
        User user = userManageService.findUser(facilitiesResult.getUser_id());
       //发送短信
        Bean4WarnMessInfo info = new Bean4WarnMessInfo(deviceResult.getCode(), module.getName(),
                facilitiesResult.getDescription(), "", user.getRealName(), user.getPhoneNumber(), msg);
        WarningMass.sendMess(info);
    }
}
